﻿using System;
//using System.Drawing;
//using System.Runtime.InteropServices;
//using System.Windows.Forms;

internal class ColorText
{
    public readonly static ConsoleColor? oldFgColor = null;
    public readonly static ConsoleColor? oldBgColor = null;

    static ColorText()
    {
        oldFgColor = Console.ForegroundColor;
        oldBgColor = Console.BackgroundColor;
    }

    private ColorText() { }

    public ConsoleColor FgColor { get; set; }

    public ConsoleColor BgColor { get; set; }

    public object ObjText { get; set; }


    public ColorText(ConsoleColor fgColor, object objText, ConsoleColor bgColor)
    {
        this.FgColor = fgColor;
        this.ObjText = objText;
        this.BgColor = bgColor;
    }

    public override string ToString()
    {
        Console.ForegroundColor = this.FgColor;
        Console.BackgroundColor = this.BgColor;

        Console.Write(this.ObjText);

        Console.ForegroundColor = oldFgColor.Value;
        Console.BackgroundColor = oldBgColor.Value;

        return string.Empty;
    }

    public void WriteLine()
    {
        Console.ForegroundColor = this.FgColor;
        Console.BackgroundColor = this.BgColor;

        Console.WriteLine(this.ObjText);

        Console.ForegroundColor = oldFgColor.Value;
        Console.BackgroundColor = oldBgColor.Value;
    }
}
public static class SharpConsoleExtension
{
    static SharpConsoleExtension()
    {
        //SetWindowPositionCenter();
    }

    #region 设置控制台窗体居中
    //private struct RECT { public int left, top, right, bottom; }

    //[DllImport("kernel32.dll", SetLastError = true)]
    //private static extern IntPtr GetConsoleWindow();
    //[DllImport("user32.dll", SetLastError = true)]
    //private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc);
    //[DllImport("user32.dll", SetLastError = true)]
    //private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);

    //private static IntPtr hWin = GetConsoleWindow();
    ///// <summary>
    ///// 控制台窗体居中
    ///// </summary>
    //private static void SetWindowPositionCenter()
    //{
    //    RECT rc;
    //    GetWindowRect(hWin, out rc);
    //    Screen scr = Screen.FromPoint(new Point(rc.left, rc.top));
    //    int x = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2;
    //    int y = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2;
    //    MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, true);
    //}
    #endregion


    #region 颜色输出
    /// <summary>
    /// 前景色：红色
    /// </summary>
    /// <param name="objText"></param>  
    /// <returns>ColorText</returns>
    public static string Red(this object objText)
    {
        return new ColorText(ConsoleColor.Red, objText, ConsoleColor.Black).ToString();
    }

    /// <summary>
    /// 前景色：绿色
    /// </summary>
    /// <param name="objText"></param>  
    /// <returns>ColorText</returns>
    public static string Green(this object objText)
    {
        return new ColorText(ConsoleColor.Green, objText, ConsoleColor.Black).ToString();
    }


    /// <summary>
    /// 前景色：蓝色
    /// </summary>
    /// <param name="objText"></param>  
    /// <returns>ColorText</returns>
    public static string Blue(this object objText)
    {
        return new ColorText(ConsoleColor.Blue, objText, ConsoleColor.Black).ToString();
    }


    /// <summary>
    /// 前景色：黄色
    /// </summary>
    /// <param name="objText"></param>  
    /// <returns>ColorText</returns>
    public static string Yellow(this object objText)
    {
        return new ColorText(ConsoleColor.Yellow, objText, ConsoleColor.Black).ToString();
    }



    /// <summary>
    /// 前景色：青色（蓝绿色）
    /// </summary>
    /// <param name="objText"></param>
    /// <returns>ColorText</returns>
    public static string Cyan(this object objText)
    {
        return new ColorText(ConsoleColor.Cyan, objText, ConsoleColor.Black).ToString();
    }

    /// <summary>
    /// 前景色：粉色(洋红色)
    /// </summary>
    /// <param name="objText"></param>
    /// <returns>ColorText</returns>
    public static string Pink(this object objText)
    {
        return new ColorText(ConsoleColor.Magenta, objText, ConsoleColor.Black).ToString();
    }


    /// <summary>
    /// 前景色：白色
    /// </summary>
    /// <param name="objText"></param>
    /// <returns>ColorText</returns>
    public static string White(this object objText)
    {
        return new ColorText(ConsoleColor.White, objText, ConsoleColor.Black).ToString();
    }

    #endregion


    /// <summary>
    /// 控制台换行
    /// </summary>
    public static void WriteLine(this object objText)
    {
        Console.WriteLine(objText);
    }

    public static void Write(this object objText)
    {
        Console.Write(objText);
    }


    #region 颜色输出 带换行
    /// <summary>
    /// 前景色：红色
    /// </summary>
    /// <param name="objText"></param>  
    /// <returns>ColorText</returns>
    public static void Red_WriteLine(this object objText)
    {
        new ColorText(ConsoleColor.Red, objText, ConsoleColor.Black).WriteLine();
    }

    /// <summary>
    /// 前景色：绿色
    /// </summary>
    /// <param name="objText"></param>  
    /// <returns>ColorText</returns>
    public static void Green_WriteLine(this object objText)
    {
        new ColorText(ConsoleColor.Green, objText, ConsoleColor.Black).WriteLine();
    }


    /// <summary>
    /// 前景色：蓝色
    /// </summary>
    /// <param name="objText"></param>  
    /// <returns>ColorText</returns>
    public static void Blue_WriteLine(this object objText)
    {
        new ColorText(ConsoleColor.Blue, objText, ConsoleColor.Black).WriteLine();
    }


    /// <summary>
    /// 前景色：黄色
    /// </summary>
    /// <param name="objText"></param>  
    /// <returns>ColorText</returns>
    public static void Yellow_WriteLine(this object objText)
    {
        new ColorText(ConsoleColor.Yellow, objText, ConsoleColor.Black).WriteLine();
    }



    /// <summary>
    /// 前景色：青色（蓝绿色）
    /// </summary>
    /// <param name="objText"></param>

    /// <returns>ColorText</returns>
    public static void Cyan_WriteLine(this object objText)
    {
        new ColorText(ConsoleColor.Cyan, objText, ConsoleColor.Black).WriteLine();
    }

    /// <summary>
    /// 前景色：粉色(洋红色)
    /// </summary>
    /// <param name="objText"></param>

    /// <returns>ColorText</returns>
    public static void Pink_WriteLine(this object objText)
    {
        new ColorText(ConsoleColor.Magenta, objText, ConsoleColor.Black).WriteLine();
    }


    /// <summary>
    /// 前景色：白色
    /// </summary>
    /// <param name="objText"></param>
    /// <returns>ColorText</returns>
    public static void White_WriteLine(this object objText)
    {
        new ColorText(ConsoleColor.White, objText, ConsoleColor.Black).WriteLine();
    }
    #endregion
}